package cn.le.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by admin on 2017/7/26.
 */
public class MutexEvenGenerator extends IntGennerator{

    private int currentEvenValue = 0;
    private Lock lock = new ReentrantLock();

    public int next()
    {
        System.out.println("next"+this.currentEvenValue);
        lock.lock(); //获取锁
        try
        {
            ++currentEvenValue;
            Thread.yield();
            ++currentEvenValue;
            return currentEvenValue;
        }
        finally {
            lock.unlock(); //释放锁
        }
    }

    public static void main(String[] args)
    {
       EvenChecker.test(new MutexEvenGenerator(),10);
    }

}
